from configobj import ConfigObj

class BaseConfig:

    def __init__(self, configfile):
        '''
        Open config file, and populate all config 
        values as class's attributes
        '''
        self._conf = ConfigObj('conf/%s.conf' % configfile)
    
    def get_conf(self, name):
        '''
        get config value
        name - str ( format: 'item' )
            or [] ( format: [section, subsection, ..., item] )
        '''
        if isinstance(name, str):
            return self._conf[name]
        elif isinstance(name, type([])):
            c = self._conf
            for n in name:
                c = c[n]
            return c
    
    def set_conf(self, name, value=''):
        '''
        set config value
        name = str or [], [] -> [section, subsection, item]
        '''
        if isinstance(name, str):
            self._conf[name] = value
        elif isinstance(name, type([])):
            if len(name) == 2:
                self._conf[name[0]][name[1]] = value
            elif len(name) == 3:
                self._conf[name[0]][name[1]][name[2]] = value
    
    def reset_conf(self, name, ctype):
        '''reset config based on datatype'''
        if ctype == 'dict':
            self._conf[name] = {}
        elif ctype == 'str':
            self._conf[name] = ''
    
    def save(self):
        '''
        save config file
        '''
        self._conf.write()
